//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package 力扣.题库.未分类;

public class demo_844 {
    public demo_844() {
    }

    public static void main(String[] args) {
        String s = "a#c";
        String t = "b";
        System.out.println(backspaceCompare(s, t));
    }

    public static boolean backspaceCompare(String s, String t) {
        if (s.length() == 1 && t.length() == 1 && s.charAt(0) == t.charAt(0)) {
            return true;
        } else if (s.length() == 1 && t.length() == 1 && s.charAt(0) != t.charAt(0)) {
            return false;
        } else {
            StringBuilder sStr = new StringBuilder();
            StringBuilder tStr = new StringBuilder();
            int sIndex = 0;
            int tIndex = 0;

            while(sIndex < s.length() || tIndex < t.length()) {
                if (sIndex < s.length() && s.charAt(sIndex) == '#') {
                    if (!sStr.isEmpty()) {
                        sStr.deleteCharAt(sStr.length() - 1);
                    }

                    ++sIndex;
                } else if (sIndex < s.length()) {
                    sStr.append(s.charAt(sIndex));
                    ++sIndex;
                }

                if (tIndex < t.length() && t.charAt(tIndex) == '#') {
                    if (!tStr.isEmpty()) {
                        tStr.deleteCharAt(tStr.length() - 1);
                    }

                    ++tIndex;
                } else if (tIndex < t.length()) {
                    tStr.append(t.charAt(tIndex));
                    ++tIndex;
                }
            }

            return sStr.toString().contentEquals(tStr);
        }
    }

    public static boolean backspaceCompare2(String s, String t) {
        int i = s.length() - 1;
        int j = t.length() - 1;
        int skipS = 0;

        for(int skipT = 0; i >= 0 || j >= 0; --j) {
            while(i >= 0) {
                if (s.charAt(i) == '#') {
                    ++skipS;
                    --i;
                } else {
                    if (skipS <= 0) {
                        break;
                    }

                    --skipS;
                    --i;
                }
            }

            while(j >= 0) {
                if (t.charAt(j) == '#') {
                    ++skipT;
                    --j;
                } else {
                    if (skipT <= 0) {
                        break;
                    }

                    --skipT;
                    --j;
                }
            }

            if (i >= 0 && j >= 0) {
                if (s.charAt(i) != t.charAt(j)) {
                    return false;
                }
            } else if (i >= 0 || j >= 0) {
                return false;
            }

            --i;
        }

        return true;
    }
}
